package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.core.domain.entity.SysUser;
import cn.sfrog.common.exception.ServiceException;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizDoctor;
import cn.sfrog.project.biz.admin.domain.vo.BizDoctorBindingAccountFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizDoctorFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizDoctorQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizDoctorVO;
import cn.sfrog.project.biz.admin.mapper.BizDoctorMapper;
import cn.sfrog.project.biz.admin.service.IBizDoctorService;
import cn.sfrog.project.biz.web.vo.index.AppDoctorDetailResultVO;
import cn.sfrog.project.biz.web.vo.index.AppDoctorManyQueryVO;
import cn.sfrog.project.biz.web.vo.index.AppDoctorQueryVO;
import cn.sfrog.project.biz.web.vo.index.AppDoctorResultVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 医生Service业务层处理
 */
@Service
public class BizDoctorServiceImpl extends ServiceImpl<BizDoctorMapper, BizDoctor> implements IBizDoctorService {
    @Resource
    private BizDoctorMapper bizDoctorMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询医生
     *
     * @param id 医生主键
     * @return 医生
     */
    @Override
    public BizDoctorVO selectBizDoctorById(Long id) {
        return bizDoctorMapper.selectBizDoctorById(id);
    }

    /**
     * 查询医生列表
     *
     * @param query 医生
     * @return 医生
     */
    @Override
    public List<BizDoctorVO> selectBizDoctorList(BizDoctorQueryVO query) {
        return bizDoctorMapper.selectBizDoctorList(query);
    }

    /**
     * 新增医生
     *
     * @param form 医生
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizDoctor(BizDoctorFormVO form) {
        BizDoctor bizDoctor = new BizDoctor();
        BeanUtils.copyProperties(form, bizDoctor);
        bizDoctor.setCreateTime(new Date());
        bizDoctor.setCreateBy(SecurityUtils.getUsername());
        bizDoctor.setUpdateTime(new Date());
        bizDoctor.setUpdateBy(SecurityUtils.getUsername());
        return bizDoctor.insert();
    }

    /**
     * 修改医生
     *
     * @param form 医生
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizDoctor(BizDoctorFormVO form) {
        BizDoctor bizDoctor = new BizDoctor();
        BeanUtils.copyProperties(form, bizDoctor);
        bizDoctor.setUpdateTime(new Date());
        bizDoctor.setUpdateBy(SecurityUtils.getUsername());
        return bizDoctor.updateById();
    }

    /**
     * 批量删除医生
     *
     * @param ids 需要删除的医生主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizDoctorByIds(Long[] ids) {
            return BizDoctor.DAO.delete(Wrappers.lambdaQuery(BizDoctor.class).in(BizDoctor::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return BizDoctor.DAO.update(Wrappers.lambdaUpdate(BizDoctor.class).in(BizDoctor::getId, (Object[]) vo.getIds()).set(BizDoctor::getStatus, vo.getStatus()));
    }

    @Override
    public List<AppDoctorResultVO> selectAppIndexDoctorList(AppDoctorQueryVO queryVO) {
        return bizDoctorMapper.selectAppIndexDoctorList(queryVO);
    }

    @Override
    public List<AppDoctorResultVO> selectAppDoctorListByDepartmentId(Long departmentId) {
        return bizDoctorMapper.selectAppDoctorListByDepartmentId(departmentId);
    }

    @Override
    public AppDoctorDetailResultVO selectAppDoctorDetail(Long id) {
        return bizDoctorMapper.selectAppDoctorDetail(id);
    }

    @Override
    public List<AppDoctorDetailResultVO> selectAppFamousDoctorList(AppDoctorQueryVO queryVO) {
        return bizDoctorMapper.selectAppFamousDoctorList(queryVO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean bindingUserAccount(BizDoctorBindingAccountFormVO formVO) {
        BizDoctor bizDoctor = BizDoctor.DAO.selectById(formVO.getId());
        if(StringUtils.isNotNull(bizDoctor.getBindingUserId())){
            throw new ServiceException("该医生已经绑定过医生账号了,不可重复绑定");
        }
        bizDoctor.setBindingUserId(formVO.getBindingUserId());
        boolean flag = bizDoctor.updateById();
        if(flag){
            SysUser sysUser = new SysUser();
            sysUser.setUserId(formVO.getBindingUserId());
            sysUser.setHospitalId(bizDoctor.getHospitalId());
            sysUser.setDepartmentId(bizDoctor.getDepartmentId());
            sysUser.setLevel(bizDoctor.getJobTitle());
            sysUser.setWorkYears(bizDoctor.getWorkYears());
            sysUserMapper.updateUser(sysUser);

        }
        return flag;
    }

    @Override
    public List<AppDoctorDetailResultVO> selectAllHospitalDoctorList(AppDoctorManyQueryVO queryVO) {
        return bizDoctorMapper.selectAllHospitalDoctorList(queryVO);
    }
}
